<?php

/**
 * Louder Cache
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file docs/LICENSE.txt.
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to info@loudertechnology.com so we can send you a copy immediately.
 *
 * @category Louder
 * @package FullyQualifiedName
 * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
 * @license New BSD License
 */

/**
 * Clase para crear FullyQualifiedNames
 *
 * @category Louder
 * @package FullyQualifiedName
 * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
 * @license New BSD License
 */
class FullyQualifiedName {

	/**
	 * Path items
	 *
	 * @var array
	 */
	private $_pathItems = array();

	/**
	 * Constructor de la clase
	 *
	 * @access public
	 * @param array $path
	 */
	public function __construct(array $path){
		$this->_pathItems = $path;
	}

	/**
	 * Devuelve los elementos internos donde debe residir el elemento
	 *
	 * @return array
	 */
	public function getPathItems(){
		return $this->_pathItems;
	}

	/**
	 * Crea un FQN apartir de un string
	 *
	 * @access public
	 * @param string $str
	 * @static
	 */
	public static function fromString($str){
		if($str!='/'){
			$length = strlen($str);
			$elements = array();
			$element = "";
			$oldchar = "";
			$j = 0;
			for($i=0;$i<$length;$i++){
				$char = substr($str, $i, 1);
				if($char!='/'&&$oldchar!="\\"){
					$element.=$char;
				} else {
					if($j==0){
						if($element!=""){
							$elements[] = $element;
						}
					} else {
						$elements[] = $element;
					}
					$element = "";
					$j++;
				}
			}
			if($j==0){
				if($element!=""){
					$elements[] = $element;
				}
			} else {
				$elements[] = $element;
			}
		} else {
			$elements = array();
		}
		return new FullyQualifiedName($elements);
	}

	/**
	 * Crea un FQN apartir de los argumentos de la funcion
	 *
	 * @access public
	 * @param array $elements
	 * @static
	 */
	public static function fromElements(){
		$elements = func_get_args();
		return new FullyQualifiedName($elements);
	}

	/**
	 * Metodo mágico __toString
	 *
	 * @return string
	 */
	public function __toString(){
		return "/".join("/", $this->_pathItems);
	}

}